<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){
	//上传用户头像
	case "setface":

		if($TS_USER['user'] == '') qiMsg("机房重地，闲人免进！");

		$userid = intval($TS_USER['user']['userid']);

		//处理目录存储方式
		$menu = substr($userid,0,1);

		$uptypes = array( 
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/x-png',
		);

		if(isset($_FILES['picfile'])){

			$f=$_FILES['picfile'];

			if(empty($f['name'])){

				qiMsg("头像不能为空！");

			}elseif ($f['name']){
				if (!in_array($_FILES['picfile']['type'],$uptypes)) {
					qiMsg("提示：你上传的头像图片类型不正确，系统仅支持 jpg,gif,png 格式的图片,点击确认返回！");
					exit;
				}
			}

			$dest_dir = 'data/uploadfile/user/'.$menu.'/'.$userid;

			createFolders($dest_dir);

			//原图
			$fileInfo=pathinfo($f['name']);
			$extension=$fileInfo['extension'];
			
			$dest=$dest_dir.'/'.mt_rand(10000,99999).'.'.$extension;

			$dest96=$dest_dir.'/'.$userid."_96.".$extension;
			$dest48=$dest_dir.'/'.$userid."_48.".$extension;
			$dest24=$dest_dir.'/'.$userid."_24.".$extension;

			move_uploaded_file($f['tmp_name'],iconv("UTF-8","gb2312",$dest));
			chmod($dest, 0755);

			include THINKSAAS.'/class.tsImg.php';

			$resizeimage = new tsImg("$dest", "96", "96", "1","$dest96");
			$resizeimage = new tsImg("$dest", "48", "48", "1","$dest48");
			$resizeimage = new tsImg("$dest", "24", "24", "1","$dest24");

			//删除原图
			unlink("$dest");

			//更新小组头像
			$DB->query("update ".dbprefix."app_user_info set face='$dest48' where userid='$userid'");

			header("Location: index.php?app=user&ac=set&ts=face");

		}

		break;

	case "setbase":
	
		$userid = $TS_USER['user']['userid'];
		$strUser = $DB->once_fetch_assoc("select username from ".dbprefix."app_user_info where userid='$userid'");
		$username = t($_POST['username']);
		
		$arrData = array(
			'username' => $username,
			'signed'		=> h($_POST['signed']),
			'mobilephone'	=> t($_POST['mobilephone']),
			'blog'			=> t($_POST['blog']),
			'about'			=> h($_POST['about']),
		);

		if($TS_USER['user'] == '') qiMsg("机房重地，闲人免进！");
		if($username == '') qiMsg("不管做什么都需要有一个名号吧^_^");
		if(strlen($username) < 4 || strlen($username) > 20) qiMsg("用户名长度必须在4到20字符之间!");
		
		if($username != $strUser['username']){
			$isusername = $DB->once_num_rows("select * from ".dbprefix."app_user_info where username='$username'");
			if($isusername > 0) qiMsg("用户名已经存在，请换个用户名！");
		}

		$DB->updateArr($arrData,"app_user_info","where userid='$userid'");

		header("Location: index.php?app=user&ac=set&ts=base");

		break;
	//修改常居地
	case "setcity":
		$arrData = array(
			'provinceid'	=> $_POST['provinceid'],
			'cityid'	=> $_POST['cityid'],
			'areaid'	=> $_POST['areaid'],
		);

		if($TS_USER['user'] == '') qiMsg("机房重地，闲人免进！");

		$userid = $TS_USER['user']['userid'];

		$DB->updateArr($arrData,"app_user_info","where userid='$userid'");
		
		$_SESSION['tsuser']['cityid'] = $_POST['cityid'];

		header("Location: index.php?app=user&ac=set&ts=city");
		break;
		
	//验证Email是否唯一
	case "inemail":
	
		$email = $_REQUEST['email'];
		$emailNum = $DB->once_num_rows("select * from ".dbprefix."app_user where `email`='".$email."'");
		
		if($emailNum > '0'):
			echo 'false';
		else:
			echo 'true';
		endif;
		
		break;
	
	//验证用户名是否唯一
	case "isusername":
		$username = $_REQUEST['username'];
		$usernameNum = $DB->once_num_rows("select * from ".dbprefix."app_user_info where `username`='".$username."'");
		
		if($usernameNum > '0'):
			echo 'false';
		else:
			echo 'true';
		endif;
		break;
		
	//验证邀请码是否使用
	case "isinvitecode":
		
		$invitecode = trim($_REQUEST['invitecode']);
		
		$codeNum = $DB->once_num_rows("select * from ".dbprefix."app_user_invites where invitecode='$invitecode' and isused='0'");
		
		if($codeNum > 0){
			echo 'true';
		}else{
			echo 'false';
		}
		
		break;
		
	//修改用户密码 
	case "setpwd":
		
		$oldpwd = trim($_POST['oldpwd']);
		$newpwd = trim($_POST['newpwd']);
		$renewpwd = trim($_POST['renewpwd']);
		
		if($oldpwd == '' || $newpwd=='' || $renewpwd=='') qiMsg("所有项都不能为空！");
		
		$userid = $TS_USER['user']['userid'];
		$strUser = $DB->once_fetch_assoc("select pwd from ".dbprefix."app_user where userid='$userid'");
		
		if(md5($oldpwd) != $strUser['pwd']) qiMsg("旧密码输入有误！");
		
		if($newpwd != $renewpwd) qiMsg("两次输入新密码密码不一样！");
		
		$DB->query("update ".dbprefix."app_user set `pwd`='".md5($newpwd)."' where userid='$userid'");
		
		qiMsg("密码修改成功！");
		
		break;
	
	//选择常住城市
	case "city":
		$provinceid = $_GET['provinceid'];
		//$arrCity = $DB->fetch_all_assoc("select * from ".dbprefix."app_location_city where provinceid = '$provinceid' and isshow='0'");
		$arrCitys = AppCacheRead('location','city.php');
		
		$arrCity = $arrCitys[$provinceid];
		
		include TmooTemplate("city");
		
		break;
	//选择区县
	case "area":
		$cityid = $_GET['cityid'];
		//$arrArea = $DB->fetch_all_assoc("select * from ".dbprefix."app_location_area where cityid = '$cityid' and isshow='0'");
		$arrAreas = AppCacheRead('location','area.php');
		$arrArea = $arrAreas[$cityid];
	
		include TmooTemplate("area");
	
		break;
	
	//用户跟随
	case "user_follow":
		$userid = $_POST['userid'];
		$userid_follow = $_POST['userid_follow'];
		
		$followNum = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid='$userid' and userid_follow='$userid_follow'");
		
		if($followNum > '0'){
			echo '0';
		}else{
			$DB->query("insert into ".dbprefix."app_user_follow (`userid`,`userid_follow`,`addtime`) values ('$userid','$userid_follow','".time()."')");
			
			//统计更新跟随和被跟随数
			//统计自己的
			$count_follow = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid='$userid'");
			$count_followed = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid_follow='$userid'");
			
			$DB->query("update ".dbprefix."app_user_info set `count_follow`='$count_follow',`count_followed`='$count_followed' where userid='$userid'");
			
			//统计别人的
			$count_follow_userid = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid='$userid_follow'");
			$count_followed_userid = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid_follow='$userid_follow'");
			
			$DB->query("update ".dbprefix."app_user_info set `count_follow`='$count_follow_userid',`count_followed`='$count_followed_userid' where userid='$userid_follow'");
			
			//发送系统消息
			$msg_userid = '0';
			$msg_touserid = $userid_follow;
			$msg_content = '恭喜，您被人跟随啦！看看他是谁吧<br />'.$TS_SITE['base']['site_url'].'index.php?app=user&ac=space&userid='.$userid;
			aac('message',$DB)->sendmsg($msg_userid,$msg_touserid,$msg_content);
			
			echo '1';
		}
		
		break;
	
	//用户取消跟随 
	case "user_nofollow":
		$userid = $_POST['userid'];
		$userid_follow = $_POST['userid_follow'];
		
		$DB->query("DELETE FROM ".dbprefix."app_user_follow WHERE userid = '$userid' AND userid_follow = '$userid_follow'");
		
		//统计更新跟随和被跟随数
		//统计自己的
		$count_follow = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid='$userid'");
		$count_followed = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid_follow='$userid'");
		
		$DB->query("update ".dbprefix."app_user_info set `count_follow`='$count_follow',`count_followed`='$count_followed' where userid='$userid'");
		
		//统计别人的
		$count_follow_userid = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid='$userid_follow'");
		$count_followed_userid = $DB->once_num_rows("select * from ".dbprefix."app_user_follow where userid_follow='$userid_follow'");
		
		$DB->query("update ".dbprefix."app_user_info set `count_follow`='$count_follow_userid',`count_followed`='$count_followed_userid' where userid='$userid_follow'");
		
		echo '1';
		
		break;
}